package com.ruoyi.project.syl_Sxl.syldept.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.syl_Sxl.syldept.domain.SylDept;
import com.ruoyi.project.syl_Sxl.syldept.service.ISylDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Security;
import java.util.List;

@RestController
@RequestMapping("/syl_Sxl/syldept")
public class SylDeptController extends BaseController {
    @Autowired
    private ISylDeptService sylDeptService;
    @PreAuthorize("@ss.hasPermi('syl_Sxl:syldept:query')")
    @GetMapping("/list")
    public TableDataInfo list(SylDept sylDept){
        //使用PageHelper处理分页查询
        startPage();
        //调用service层方法
        List<SylDept> list = sylDeptService.selectList(sylDept);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('syl_Sxl:syldept:add')")
    @Log(title = "科室管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SylDept sylDept){
        sylDept.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sylDeptService.insertDept(sylDept));
    }

    /**
     * 根据科室ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('syl_Sxl:syldept:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable Long deptId)
    {
        return AjaxResult.success(sylDeptService.selectById(deptId));
    }

    //修改科室信息
    @PreAuthorize("@ss.hasPermi('syl_Sxl:syldept:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SylDept sylDept)
    {
        sylDept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sylDeptService.updateDept(sylDept));
    }

    //删除科室信息
    @PreAuthorize("@ss.hasPermi('syl_Sxl:syldept:remove')")
    @DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds)
    {
        return toAjax(sylDeptService.deleteByIds(deptIds));
    }
    /**
     * 获取科室选择框列表
     */
    @GetMapping("/deptSelect")
    public AjaxResult optionselect()
    {
        List<SylDept> list = sylDeptService.selectAll();
        return AjaxResult.success(list);
    }

}
